local MainScene = class("MainScene", cc.load("mvc").ViewBase)

local visibleSize = cc.Director:getInstance():getVisibleSize()

local origin = cc.Director:getInstance():getVisibleOrigin()

local bugFlag = 0
local bugTime = 0

local function movingBug(  )        --来回动作
    local bugFrame_Width = 200
    local bugFrame_Height = 200

    local textureBug = cc.Director:getInstance():getTextureCache():addImage("BugAnt.png")      --纹理缓存
    local rect = cc.rect(0, 0, bugFrame_Width, bugFrame_Height)
    local frame1 = cc.SpriteFrame:createWithTexture(textureBug, rect)
    rect = cc.rect(bugFrame_Width, 0, bugFrame_Width + 100, bugFrame_Height )
    local frame2 = cc.SpriteFrame:createWithTexture(textureBug, rect)

    local spriteBug = cc.Sprite:createWithSpriteFrame(frame1)
    -- spriteBug.isPaused = false
    spriteBug:setPosition(origin.x, origin.y + visibleSize.height/4*3)

    local animation = cc.Animation:createWithSpriteFrames({frame1, frame2}, 0.5)
    local animate = cc.Animate:create(animation)
    spriteBug:runAction(cc.RepeatForever:create(animate))

    local function tick(  )
        if spriteBug.isPaused then return end
        local x, y = spriteBug:getPosition()
        if x > origin.x + visibleSize.width then 
            bugFlag = 1
            bugTime = bugTime + 1
        elseif x<origin.x then
            bugFlag = 0
            bugTime = bugTime + 1
        end

        if bugFlag == 1 and bugTime > 0 then
            x = x - 1
            spriteBug:setFlippedX(true)
        elseif bugFlag == 0 and bugTime > 0 then
            x = x + 1
            spriteBug:setFlippedX(false)
        else
            x = x + 1
        end

        spriteBug:setPositionX(x)
    end     --end tick
    cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0 , false)
    return spriteBug 

end     --end movingBug

function MainScene:onCreate()

    -- add background image - sprite
    local bg = cc.Sprite:create("Black.jpg")
    bg:setPosition(display.center)
    self:addChild(bg, 0)

    -- add play button - menu
    local playButton = cc.MenuItemImage:create("PlayButton.png", "PlayButton.png")
        :onClicked(function()
            self:getApp():enterScene("GameScene")
        end)
    cc.Menu:create(playButton)
        :move(display.cx, display.cy - 200)
        :addTo(self)

    -- local particleSystem = cc.ParticleFire:create()
    -- -- particleSystem:setPosition(cc.Director:getInstance():convertToGL(cc.p(0, 0)))
    -- particleSystem:setPosition(display.cx + 100, display.cy + 100)
    -- self:addChild(particleSystem, 1)

    --add label
    -- cc.Label:createWithSystemFont("Hello World", "Arial", 40)
    --     :move(display.cx, display.cy + 290)
    --     :addTo(self)

    -- local bug1 = cc.Sprite:create("BugAnt.png", cc.rect(0, 0, 200, 200))
    -- bug1:setPosition(cc.p(200, 200))
    -- self:addChild(bug1, 1)

    -- local bug2 = cc.Sprite:create("BugAnt.png", cc.rect(200, 0, 300, 200))
    -- bug2:setPosition(cc.p(400, 400))
    -- self:addChild(bug2, 1)

    -- local createBug = movingBug()
    -- self:addChild(createBug, 1)

end     --end MainScene

return MainScene
